#include "rgslayertreeviewmenuprovider.h"
#include"Riskgis.h"

#include <QMenu>
#include <QModelIndex>
#include <QIcon>
#include <QAction>
#include <QMap>

// QGis include
#include <qgslayertreeviewdefaultactions.h>
#include <qgslayertreenode.h>
#include <qgslayertreemodel.h>
#include <qgslayertree.h>
#include <qgsrasterlayer.h>

rgslayertreeviewmenuprovider::rgslayertreeviewmenuprovider(QgsLayerTreeView *view, QgsMapCanvas* canvas)
    : mView( view )
    , mCanvas( canvas )
{

}

rgslayertreeviewmenuprovider::~rgslayertreeviewmenuprovider()
{

}

QMenu *rgslayertreeviewmenuprovider::createContextMenu()
{
    QMenu* menu = new QMenu();

    QgsLayerTreeViewDefaultActions* actions = mView->defaultActions();

    QModelIndex idx = mView->currentIndex();

    // global menu
    if ( !idx.isValid() )
    {

        menu->addAction( actions->actionAddGroup( menu ) );
        menu->addAction( QIcon(":/images/themes/default/mActionExpandTree.svg"), tr( "&Expand All" ), mView, SLOT( expandAll() ) );
        menu->addAction( QIcon(":/images/themes/default/mActionCollapseTree.svg"), tr( "&Collapse All" ), mView, SLOT( collapseAll() ) );
        menu->addSeparator();
    }
    else if ( QgsLayerTreeNode* node = mView->currentNode() )
    {
        // layer or group selected
        if ( QgsLayerTree::isGroup( node ) )
        {
            menu->addAction( actions->actionZoomToGroup( mCanvas, menu ) );
            menu->addAction( QIcon(":/images/themes/default/mActionRemove.svg"), tr( "&Remove" ), Riskgis::instance(), SLOT( on_actionRemoveGroupOrLayer_triggered() ) );
            menu->addAction( actions->actionRenameGroupOrLayer( menu ) );
            if ( mView->selectedNodes( true ).count() >= 2 )
            {
                menu->addAction( actions->actionGroupSelected( menu ) );
            }
            menu->addAction( actions->actionAddGroup( menu ) );
        }
        else if ( QgsLayerTree::isLayer( node ) )
        {
            QgsMapLayer* layer = QgsLayerTree::toLayer( node )->layer();
            menu->addAction( QIcon(":/images/themes/default/mActionRemove.svg"), tr( "&Remove" ), Riskgis::instance(), SLOT( on_actionRemoveGroupOrLayer_triggered()));
            menu->addAction( QIcon(":/images/themes/default/propertyicons/editmetadata.svg"), tr( "&Rename" ), Riskgis::instance(), SLOT( on_actionRenameGroupOrLayer_triggered()));
            menu->addAction( actions->actionZoomToLayers( mCanvas, menu ) );
            menu->addAction( actions->actionShowInOverview( menu ) );

            // 属性表菜单
            //menu->addAction( QIcon(":/images/themes/default/mActionOpenTable.svg"), tr( "&Open Attribute Table" ), Riskgis::instance(), SLOT( openAttributeTableDialog() ) );

            // 如果选择的是栅格图层
            if ( layer && layer->type() == QgsMapLayerType::RasterLayer )
            {
                //menu->addAction( tr( "&Zoom to Best Scale (100%)" ), Riskgis::instance(), SLOT( legendLayerZoomNative() ) );

                QgsRasterLayer* rasterLayer =  qobject_cast<QgsRasterLayer *>( layer );
                if ( rasterLayer && rasterLayer->rasterType() != QgsRasterLayer::Palette )
                {
                   // menu->addAction( tr( "&Stretch Using Current Extent" ), Riskgis::instance(), SLOT( legendLayerStretchUsingCurrentExtent() ) );
                }
            }
            if (layer && layer->type() ==QgsMapLayerType::VectorLayer)
            {

            }

            //menu->addAction( QIcon(":/images/themes/default/mActionRemove.svg"), tr( "&Remove" ), Riskgis::instance(), SLOT( removeLayer() ) );

            // 样式表
            //menu->addAction( QIcon(":/images/themes/default/mIconProperties.svg"), tr( "&Property" ), Riskgis::instance(), SLOT( showProperty() ) );
        }
    }

    return menu;
}

void rgslayertreeviewmenuprovider::addLegendLayerAction(QAction *action, QString menu, QString id, QgsMapLayerType type, bool allLayers)
{

}

bool rgslayertreeviewmenuprovider::removeLegendLayerAction(QAction *action)
{
    return false;
}

void rgslayertreeviewmenuprovider::addLegendLayerActionForLayer(QAction *action, QgsMapLayer *layer)
{

}

void rgslayertreeviewmenuprovider::removeLegendLayerActionsForLayer(QgsMapLayer *layer)
{

}

void rgslayertreeviewmenuprovider::addCustomLayerActions(QMenu *menu, QgsMapLayer *layer)
{

}
